﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;


namespace MisClient
{
    public partial class FrmWholesale : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();

        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();

        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();

        private IBLL.ISys0 sys0BLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSys0();

        private IBLL.ICusDep cusDepBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusDep();

        private IBLL.ICusShop cusShopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusShop();

        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();

        private IBLL.IFkType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();

        private IBLL.IGoodsCus goodsCusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoodsCus();

        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();

        private bool isOpen = false;
        private WinPrint winPrint = new WinPrint();

        private int dataGridView = 0;
        private int left = 51;      //左边距
        private int btHeight = 26;     //表头高度　包括商品编号　仓库等等

        private int pageSize = 7;

        public FrmWholesale()
        {
            InitializeComponent();
        }

        public FrmWholesale(bool isOpen)
        {
            InitializeComponent();
            this.isOpen = isOpen;
        }

        private void FrmWholesale_Load(object sender, EventArgs e)
        {
            //销售部门初始化
            this.cBoxDep.ValueMember = "DEP_ID";
            this.cBoxDep.DisplayMember = "DEPART";
            this.cBoxDep.DataSource = departBLL.GetAll();
            this.cBoxDep.Text = "";


            //结算类型初始化
            this.cBoxFK.ValueMember = "FK_id";
            this.cBoxFK.DisplayMember = "Fktype";
            this.cBoxFK.DataSource = typeBLL.FindAll();
            this.cBoxFK.Text = "";

            //提货仓初始化
            this.cBoxShop.ValueMember = "SHOP_ID";
            this.cBoxShop.DisplayMember = "SHOP";
            this.cBoxShop.DataSource = shopBLL.GetAll();
            this.cBoxShop.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtDDNo.Text = "0";
            this.txtNo.Clear();
            this.txtPNo.Clear();
            this.txtOperUser.Text = accountUser.UserCode;
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.txtDepCode.Text = accountUser.DeptCode;
            this.cBoxDep.SelectedValue = accountUser.DeptCode;
            this.txtCusCode.Clear();
            this.txtCusName.Clear();
            this.txtCusDepCode.Clear();
            this.cBoxCusDep.Text = "";
            this.txtCusShopCode.Clear();
            this.cBoxCusShop.Text = "";
            this.txtFKID.Text = "XJ";
            this.cBoxFK.SelectedValue = this.txtFKID.Text;
            this.txtShopCode.Text = "01";
            this.cBoxShop.SelectedValue = this.txtShopCode.Text;
            this.dtpDateJs.Text = DateTime.Now.ToShortDateString();
            this.txtOper.Text = accountUser.Oper;
            this.txtDateFK.Text = DateTime.Now.ToShortDateString();
            this.txtBei.Clear();
            this.txtTax.Text = "17";
            this.chkF.Checked = false;
            this.dataGridView1.Rows.Clear();

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL0.Clear();
            this.txtYcsl.Clear();
            this.txtSl2.Clear();
            this.txtSl3.Clear();
            this.txtSL_P.Clear();
            this.txtUnit_P.Clear();
            this.txtPack.Clear();
            this.txtTax1.Clear();
            this.txtLastPrice.Clear();
            this.txtSL.Clear();
            this.txtUnit.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtTotalNum.Clear();
            this.txtTotalQuantity.Clear();
            this.txtSumJe.Clear();
            this.txtNoSumJe.Clear();
            this.txtMsg1.Clear();
            this.txtMsg2.Clear();

            this.txtDDNo.Enabled = true;
            this.txtDepCode.Enabled = true;
            this.cBoxDep.Enabled = true;
            this.txtCusCode.Enabled = true;
            this.txtCusName.Enabled = true;
            this.txtCusName.ReadOnly = true;
            this.txtCusDepCode.Enabled = true;
            this.cBoxCusDep.Enabled = true;
            this.txtCusShopCode.Enabled = true;
            this.cBoxCusShop.Enabled = true;
            this.txtFKID.Enabled = true;
            this.cBoxFK.Enabled = true;
            this.txtShopCode.Enabled = true;
            this.cBoxShop.Enabled = true;
            this.dtpDateJs.Enabled = true;
            this.txtOper.Enabled = true;
            this.txtBei.Enabled = true;
            this.txtTax.Enabled = true;
            this.chkF.Enabled = true;

            this.btnAdd.Enabled = false;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;

            //-------------
            this.btnPrint.Enabled = true;



            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = true;
            this.txtDDNo.Focus();
        }


        private void txtDDNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    this.cBoxDep.SelectedValue = this.txtDepCode.Text.Trim();
                    if (this.cBoxDep.SelectedValue == null || this.cBoxDep.SelectedValue.ToString().Trim().Length < 2)
                    {
                        MessageBox.Show("部门代码应为2位,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtDepCode.Focus();
                    }
                    else
                    {
                        this.txtCusCode.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                }
                
            }
        }

        private void cBoxDep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxDep.SelectedValue != null)
                this.txtDepCode.Text = this.cBoxDep.SelectedValue.ToString().Trim();
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MisEntity.Customer entity = cusBLL.Load(this.txtCusCode.Text.Trim());
                    if (entity != null)
                    {
                        this.txtCusName.Text = entity.CUSTOMER.Trim();

                        //客户代码部门初始化
                        this.cBoxCusDep.ValueMember = "CUS_DEP";
                        this.cBoxCusDep.DisplayMember = "DEPART";
                        this.cBoxCusDep.DataSource = cusDepBLL.FindBy(entity.CUS_ID.Trim());

                        //客户分店初始化
                        this.cBoxCusShop.ValueMember = "CUS_SHOP";
                        this.cBoxCusShop.DisplayMember = "SHOP";
                        this.cBoxCusShop.DataSource = cusShopBLL.FindBy(entity.CUS_ID.Trim());
                        this.txtCusDepCode.Focus();
                    }
                    else
                    {
                        this.txtCusCode.Focus();
                    }
                    
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusCode.Focus();
                }
            }
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
                {
                    this.cBoxCusDep.SelectedValue = this.txtCusDepCode.Text.Trim();
                    if (this.cBoxCusDep.SelectedValue == null) 
                    {
                        MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusDepCode.Focus();
                    }
                    else
                    {
                        this.txtCusShopCode.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusDepCode.Focus();
                }
            }
        }

        private void cBoxCusDep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxCusDep.SelectedValue != null)
                this.txtCusDepCode.Text = this.cBoxCusDep.SelectedValue.ToString().Trim();
        }

        private void txtCusShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
                {
                    this.cBoxCusShop.SelectedValue = this.txtCusShopCode.Text.Trim();
                    if (this.cBoxCusShop.SelectedValue == null)
                    {
                        MessageBox.Show("客户分店代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusShopCode.Focus();
                    }
                    else
                    {
                        this.txtFKID.Focus();
                    }
                }
                else 
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusShopCode.Focus();
                }
                
            }
        }

        private void cBoxCusShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxCusShop.SelectedValue != null)
                this.txtCusShopCode.Text = this.cBoxCusShop.SelectedValue.ToString().Trim();
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtFKID.Text))
                {
                    this.cBoxFK.SelectedValue = this.txtFKID.Text.Trim().ToUpper();
                }
                this.txtShopCode.Focus();
            }
        }

        private void cBoxFK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxFK.SelectedValue != null)
                this.txtFKID.Text = this.cBoxFK.SelectedValue.ToString().Trim();
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
                {
                    this.cBoxShop.SelectedValue = this.txtShopCode.Text.Trim().ToUpper();
                    if (this.cBoxShop.SelectedValue == null)
                    {
                        MessageBox.Show("提货仓代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtShopCode.Focus();
                    }
                    else
                        this.txtTax.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                }
                
            }
        }

        private void cBoxShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxShop.SelectedValue != null)
                this.txtShopCode.Text = this.cBoxShop.SelectedValue.ToString().Trim();
        }

        private void txtTax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.dtpDateJs.Focus();
            }
        }

        private void dtpDateJs_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOper.Focus();
            }
        }

        private void txtOper_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBei.Focus();
            }
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.chkF.Focus();
            }
        }

        private void chkF_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDDNo.Text.Trim()))
                {
                    MessageBox.Show("原始单号不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDDNo.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()) || this.cBoxDep.SelectedValue == null || this.cBoxDep.SelectedValue.ToString().Trim().Length < 2
                    || !this.txtDepCode.Text.Trim().Equals(this.cBoxDep.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show("部门代码应为2位,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCusCode.Text.Trim()) || string.IsNullOrEmpty(this.txtCusName.Text.Trim()))
                {
                    MessageBox.Show("客户代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()) || this.cBoxCusDep.SelectedValue == null ||
                    !this.txtCusDepCode.Text.Trim().Equals(this.cBoxCusDep.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusDepCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()) || this.cBoxCusShop.SelectedValue == null ||
                    !this.txtCusShopCode.Text.Trim().Equals(this.cBoxCusShop.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show("客户分店代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtCusShopCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtShopCode.Text.Trim()) || this.cBoxShop.SelectedValue == null ||
                    !this.txtShopCode.Text.Trim().Equals(this.cBoxShop.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show("提货仓代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtOper.Text.Trim()))
                {
                    MessageBox.Show("人员不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtOper.Focus();
                    return;
                }
                this.txtDDNo.Enabled = false;
                this.txtDepCode.Enabled = false;
                this.cBoxDep.Enabled = false;
                this.txtCusCode.Enabled = false;
                this.txtCusName.Enabled = false;
                this.txtCusName.ReadOnly = true;
                this.txtCusDepCode.Enabled = false;
                this.cBoxCusDep.Enabled = false;
                this.txtCusShopCode.Enabled = false;
                this.cBoxCusShop.Enabled = false;
                this.txtFKID.Enabled = false;
                this.cBoxFK.Enabled = false;
                this.txtShopCode.Enabled = false;
                this.cBoxShop.Enabled = false;
                this.dtpDateJs.Enabled = false;
                this.txtOper.Enabled = false;
                this.txtBei.Enabled = false;
                this.txtTax.Enabled = false;
                this.chkF.Enabled = false;

                this.txtID.Enabled = true;
                this.txtSL0.Enabled = true;
                this.txtYcsl.Enabled = true;
                this.txtSl2.Enabled = true;
                this.txtSl3.Enabled = true;
                this.txtSL_P.Enabled = true;
                this.txtUnit_P.Enabled = true;
                this.txtPack.Enabled = true;
                this.txtTax1.Enabled = true;
                this.txtLastPrice.Enabled = true;
                this.txtSL.Enabled = true;
                this.txtUnit.Enabled = true;

                if (isOpen)
                {
                    this.txtPrice.Enabled = true;
                    this.txtJe.Enabled = true;
                }

                this.btnAdd.Enabled = false;
                this.btnAddRecord.Enabled = true;
                this.btnDelRecord.Enabled = true;

                //----------------false
                this.btnPrint.Enabled = true;


                this.btnRef.Enabled = true;
                this.btnQuit.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnCan.Enabled = true;
                this.txtID.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (this.txtID.Text.Trim().Length < 4 || this.txtID.Text.Trim().Length > 13)
                {
                    MessageBox.Show(string.Format("当前商品条码只有{0}位，请重新输入4到13位的商品条码！", this.txtID.Text.Trim().Length), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    MisEntity.Goods entity = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (entity == null)
                    {
                        MessageBox.Show("无此商品，请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        StringBuilder ErrMsg = new StringBuilder();
                        if (entity.ycsl <= 0)
                        {
                            MessageBox.Show("此商品库存不足，请注意！\n", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }

                        MisEntity.GoodsCus GoodsCusEntity = goodsCusBLL.Find(this.txtCusCode.Text.Trim(), entity.ID.Trim());
                        //if (GoodsCusEntity == null)
                        //{
                            //ErrMsg.Append("此商品未在此客户处定价，请先设置！\n");
                        //}
                        //else
                        //{
                            //if (this.cBoxDep.SelectedValue != null && !GoodsCusEntity.DEP_ID.Equals(this.cBoxDep.SelectedValue.ToString().Trim()))
                            //{
                            //    ErrMsg.Append(string.Format("此商品所属部门为{0}，不可操作！\n", GoodsCusEntity.DEP_ID));
                            //}

                            //if (this.cBoxCusDep.SelectedValue != null && !GoodsCusEntity.CUS_DEP.Equals(this.cBoxCusDep.SelectedValue.ToString().Trim()))
                            //{
                            //    ErrMsg.Append(string.Format("此商品在此客户处所属部门已设为{0}，不可操作！\n", GoodsCusEntity.CUS_DEP));
                            //}

                            //if (this.cBoxCusShop.SelectedValue != null && !GoodsCusEntity.CUS_SHOP.Equals(this.cBoxCusShop.SelectedValue.ToString().Trim()))
                            //{
                            //    ErrMsg.Append(string.Format("此商品在此客户处所属分店已设为{0}，不可操作！\n", GoodsCusEntity.CUS_SHOP));
                            //}
                        //    this.txtPrice.Text = GoodsCusEntity.PRICE_L.ToString();
                        //    this.txtLastPrice.Text = this.txtPrice.Text;
                        //}

                        if (!string.IsNullOrEmpty(ErrMsg.ToString()))
                        {
                            MessageBox.Show(ErrMsg.ToString(), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }

                        if (GoodsCusEntity != null)
                        {
                            if (this.cBoxFK.SelectedValue != null && !GoodsCusEntity.TYPE_JS.Equals(this.cBoxFK.SelectedValue.ToString().Trim()))
                            {
                                if (MessageBox.Show(string.Format("此商品在此客户处结算方式应是{0}，是否继续[Y/N]？", GoodsCusEntity.TYPE_JS), "提问窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    this.cBoxFK.SelectedValue = GoodsCusEntity.TYPE_JS;
                                    this.txtMsg1.Text = string.Format("本单结算方式已改为{0}，请注意！", GoodsCusEntity.TYPE_JS);
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                        this.txtID.Text = entity.ID.Trim();
                        this.txtDM.Text = entity.DM.Trim();
                        this.txtGoods.Text = entity.GOODS.Trim();
                        this.txtUnit_P.Text = entity.UNIT_P.Trim();
                        this.txtPack.Text = entity.PACK.ToString();
                        this.txtTax.Text = entity.Tax.ToString();
                        this.txtUnit.Text = entity.UNIT.Trim();
                        if (GoodsCusEntity != null)
                        {
                            this.txtPrice.Text = GoodsCusEntity.PRICE_L.ToString();
                        }
                        else if (entity.PRICE1 > 0)
                        {
                            this.txtPrice.Text = String.Format("{0:N4}", entity.PRICE1);
                        }
                        else if (entity.Je_last > 0 && entity.SL_last > 0)
                        {
                            this.txtPrice.Text = String.Format("{0:N4}", entity.Je_last / entity.SL_last);
                        }
                        this.txtLastPrice.Text = this.txtPrice.Text;
                        this.txtSL0.Text = decimal.ToInt32((decimal)entity.SL0).ToString();
                        this.txtYcsl.Text = decimal.ToInt32((decimal)entity.SL1).ToString();
                        this.txtSl2.Text = decimal.ToInt32((decimal)entity.SL2).ToString();
                        this.txtSl3.Text = decimal.ToInt32((decimal)entity.SL3).ToString();
                        this.txtSL_P.Focus();
                    }

                }
            }
        }

        private void txtSL_P_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtSL_P.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL_P.Text, out n) && decimal.TryParse(this.txtPack.Text, out n))
                    {
                        this.txtSL.Text = String.Format("{0:N2}", decimal.Parse(this.txtPack.Text.Trim()) * decimal.Parse(this.txtSL_P.Text.Trim()));
                        this.txtSL.Focus();
                    }
                }

            }
        }

        private void txtSL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPack.Text, out n))
                    {
                        this.txtSL_P.Text = String.Format("{0:N0}", decimal.Parse(this.txtSL.Text.Trim()) / decimal.Parse(this.txtPack.Text.Trim()));
                    }
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                        this.txtUnit.Focus();
                    }
                }
            }
        }


        protected void CalculateTotal()
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                decimal TotalNum = 0;
                decimal TotalQuantity = 0;
                decimal TotalSumJe = 0;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    dr = this.dataGridView1.Rows[i];
                    TotalNum += decimal.Parse(dr.Cells["SL_P"].Value.ToString());
                    TotalQuantity += decimal.Parse(dr.Cells["SL"].Value.ToString());
                    TotalSumJe += decimal.Parse(dr.Cells["JE"].Value.ToString());
                }
                this.txtTotalNum.Text = decimal.ToInt32(TotalNum).ToString();
                this.txtTotalQuantity.Text = decimal.ToInt32(TotalQuantity).ToString();
                this.txtSumJe.Text = String.Format("{0:N2}", TotalSumJe);
                this.txtNoSumJe.Text = String.Format("{0:N4}", TotalSumJe / decimal.Parse("1." + this.txtTax.Text.Trim()));
            }
            else
            {
                this.txtTotalNum.Clear();
                this.txtTotalQuantity.Clear();
                this.txtSumJe.Clear();
                this.txtNoSumJe.Clear();
            }
        }

        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtID.Focus();
                return;
            }
            else
            {
                if (string.IsNullOrEmpty(this.txtGoods.Text.Trim()))
                {
                    MessageBox.Show("请先确认商品！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtID.Focus();
                    return;
                }
                MisEntity.Goods entity = goodsBLL.FindByCode(this.txtID.Text.Trim());
                if (entity == null)
                {
                    MessageBox.Show("无此商品，请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtID.Focus();
                    return;
                }
                else
                {
                    if (!entity.DM.Trim().Equals(this.txtDM.Text.Trim()))
                    {
                        MessageBox.Show("两次设置的商品不一致，请重新设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                }
            }

            if (string.IsNullOrEmpty(this.txtSL.Text.Trim()))
            {
                MessageBox.Show("请输入数量不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSL.Focus();
                return;
            }

            //if (decimal.Parse(this.txtSL.Text.Trim()) < 0)
            //{
            //    MessageBox.Show("数量不能为负数！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    this.txtSL.Focus();
            //    return;
            //}

            String[] DataRow = new String[14];
            DataRow[0] = this.txtID.Text.Trim();
            DataRow[1] = this.txtDM.Text.Trim();
            DataRow[2] = this.txtGoods.Text.Trim();
            DataRow[3] = this.txtUnit.Text.Trim();
            DataRow[4] = this.txtSL.Text.Trim();
            DataRow[5] = this.txtPrice.Text.Trim();
            DataRow[6] = this.txtJe.Text.Trim();
            DataRow[7] = this.txtSL_P.Text.Trim();
            DataRow[8] = "";
            DataRow[9] = this.txtTax.Text.Trim();
            DataRow[10] = this.txtPack.Text.Trim();
            DataRow[11] = String.Format("{0:N4}", decimal.Parse(DataRow[5]) / decimal.Parse("1." + DataRow[9]));
            DataRow[12] = String.Format("{0:N4}", decimal.Parse(DataRow[6]) / decimal.Parse("1." + DataRow[9]));
            DataRow[13] = DataRow[5];
            this.dataGridView1.Rows.Add(DataRow);

            CalculateTotal();

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL0.Clear();
            this.txtYcsl.Clear();
            this.txtSl2.Clear();
            this.txtSl3.Clear();
            this.txtSL_P.Clear();
            this.txtUnit_P.Clear();
            this.txtPack.Clear();
            //this.txtTax.Clear();
            this.txtLastPrice.Clear();
            this.txtSL.Clear();
            this.txtUnit.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtMsg1.Clear();
            this.txtMsg2.Clear();
            this.txtID.Focus();
        }

        private void btnDelRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.dataGridView1.Rows.Remove(this.dataGridView1.CurrentRow);
                CalculateTotal();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.txtDDNo.Enabled = false;
            this.txtDepCode.Enabled = false;
            this.cBoxDep.Enabled = false;
            this.txtCusCode.Enabled = false;
            this.txtCusName.Enabled = false;
            this.txtCusName.ReadOnly = true;
            this.txtCusDepCode.Enabled = false;
            this.cBoxCusDep.Enabled = false;
            this.txtCusShopCode.Enabled = false;
            this.cBoxCusShop.Enabled = false;
            this.txtFKID.Enabled = false;
            this.cBoxFK.Enabled = false;
            this.txtShopCode.Enabled = false;
            this.cBoxShop.Enabled = false;
            this.dtpDateJs.Enabled = false;
            this.txtOper.Enabled = false;
            this.txtBei.Enabled = false;
            this.txtTax.Enabled = false;
            this.chkF.Enabled = false;

            this.txtID.Enabled = false;
            this.txtDM.Enabled = false;
            this.txtGoods.Enabled = false;
            this.txtSL0.Enabled = false;
            this.txtYcsl.Enabled = false;
            this.txtSl2.Enabled = false;
            this.txtSl3.Enabled = false;
            this.txtSL_P.Enabled = false;
            this.txtUnit_P.Enabled = false;
            this.txtPack.Enabled = false;
            this.txtTax.Enabled = false;
            this.txtLastPrice.Enabled = false;
            this.txtSL.Enabled = false;
            this.txtUnit.Enabled = false;
            this.txtPrice.Enabled = false;
            this.txtJe.Enabled = false;
            this.txtMsg1.Enabled = false;
            this.txtMsg2.Enabled = false;
            this.txtPrice.Enabled = false;
            this.txtTax1.Enabled = false;

            this.btnAdd.Enabled = true;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = true;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = false;
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请检查供应商，\n进价等项目是否正确。\n是否存盘[Y/N]？", "提问窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.btnCan.Enabled = false;
                this.btnSave.Enabled = false;
                if (string.IsNullOrEmpty(this.txtTotalNum.Text))
                {
                    MessageBox.Show("合计为零不能保存！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.btnCan.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtDDNo.Text))
                {
                    MessageBox.Show("原始单号不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtDDNo.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtDepCode.Text))
                {
                    MessageBox.Show("部门不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtDepCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtCusCode.Text) || string.IsNullOrEmpty(this.txtCusDepCode.Text) || string.IsNullOrEmpty(this.txtCusShopCode.Text))
                {
                    MessageBox.Show("客户不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtCusCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtShopCode.Text))
                {
                    MessageBox.Show("提货仓不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtShopCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtOper.Text))
                {
                    MessageBox.Show("人员不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtOper.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("进货部门代码不存在，或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.btnCan.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.txtDepCode.Focus();
                    return;
                }
                if (this.dataGridView1.Rows.Count > 0)
                {
                    IList<MisEntity.Sacf> list = new List<MisEntity.Sacf>(this.dataGridView1.Rows.Count);
                    MisEntity.Sacf entity = null;
                    DataGridViewRow dr = null;

                    string pid = sys0BLL.GetNoSac();
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        dr = this.dataGridView1.Rows[i];
                        entity = new MisEntity.Sacf();
                        entity.NO = pid;
                        entity.DEP_ID = this.cBoxDep.SelectedValue.ToString().Trim();
                        entity.CUS_ID = this.txtCusCode.Text.Trim();
                        entity.CUS_DEP = this.cBoxCusDep.SelectedValue.ToString().Trim();
                        entity.CUS_SHOP = this.cBoxCusShop.SelectedValue.ToString().Trim();
                        entity.SHOP_ID = this.cBoxShop.SelectedValue.ToString().Trim();
                        entity.TAX = short.Parse(dr.Cells["TAX"].Value.ToString());
                        entity.OPER = this.txtOper.Text.Trim();
                        entity.BEI = this.txtBei.Text.Trim();
                        entity.ID = dr.Cells["ID"].Value.ToString();
                        entity.DM = dr.Cells["DM"].Value.ToString();
                        entity.UNIT = dr.Cells["UNIT"].Value.ToString();
                        entity.SL = decimal.Parse(dr.Cells["SL"].Value.ToString());
                        entity.PRICE = decimal.Parse(dr.Cells["PRICE"].Value.ToString());
                        entity.PRICE_HS = decimal.Parse(dr.Cells["PRICE_L"].Value.ToString());
                        entity.JE_HS = decimal.Parse(dr.Cells["JE"].Value.ToString());
                        entity.SL_P = int.Parse(dr.Cells["SL_P"].Value.ToString());
                        entity.PACK = short.Parse(dr.Cells["PACK"].Value.ToString());
                        entity.PRICE_FS = decimal.Parse(dr.Cells["NOPRICE"].Value.ToString());
                        entity.JE_FS=decimal.Parse(dr.Cells["NOJE"].Value.ToString());
                        if(this.chkF.Checked) entity.F2 =(char)1;
                        entity.DDNO = this.txtDDNo.Text.Trim();
                        entity.PRICE_JS = decimal.Parse(dr.Cells["PRICE_L"].Value.ToString());
                        list.Add(entity);
                    }
                    sacfBLL.Insert(list);
                    this.btnSave.Enabled = false;
                    this.btnCan.Enabled = false;
                    this.txtNo.Text = pid;
                    if (MessageBox.Show(string.Format("单据已存盘，编号：{0}，总金额：{1}。是否打印[y/n]？", pid, this.txtSumJe.Text.Trim()), "提问窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        PageSettings df = new PageSettings();
                        PageSettings df1 = this.printDocument1.DefaultPageSettings;
                        df.PaperSize = new PaperSize("new size", 902, 366);
                        this.printDocument1.DefaultPageSettings = df;

                        this.dataGridView = 0;
                        this.printDocument1.Print();
                    }

                    this.txtID.Enabled = false;
                    this.txtSL0.Enabled = false;
                    this.txtYcsl.Enabled = false;
                    this.txtSl2.Enabled = false;
                    this.txtSl3.Enabled = false;
                    this.txtSL_P.Enabled = false;
                    this.txtUnit_P.Enabled = false;
                    this.txtPack.Enabled = false;
                    this.txtTax1.Enabled = false;
                    this.txtLastPrice.Enabled = false;
                    this.txtSL.Enabled = false;
                    this.txtUnit.Enabled = false;
                    this.txtPrice.Enabled = false;
                    
                    this.btnAddRecord.Enabled = false;
                    this.btnDelRecord.Enabled = false;
                    this.btnAdd.Enabled = true;
                    this.btnPrint.Enabled = true;
                    this.btnRef.Enabled = true;
                    this.btnQuit.Enabled = true;
                }
            }
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                PageSettings df = new PageSettings();
                PageSettings df1 = this.printDocument1.DefaultPageSettings;
                df.PaperSize = new PaperSize("new size", 902, 366);
                this.printDocument1.DefaultPageSettings = df;

                this.dataGridView = 0;
                this.printPreviewDialog1.Document = this.printDocument1;
                this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
                this.printPreviewDialog1.ShowDialog();
            }
            catch
            {
                MessageBox.Show("没有检测到打印机设备!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            webPrint1(e);
        }
        private void webPrint1(System.Drawing.Printing.PrintPageEventArgs e)
        {
            int fontLeft = 42;

            Font _ContentFont = new Font("宋体", 10, FontStyle.Bold);
            Font _GoodsFont = new Font("宋体", 11, FontStyle.Bold);
            Point _CurrentPoint = new Point(fontLeft + left, 60);     //标题位置
            e.Graphics.DrawString((string.Format("{0} {1}", this.txtCusCode.Text.Trim(), this.txtCusName.Text.Trim())), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 488;
            e.Graphics.DrawString(this.txtDate.Text.Trim(), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 720;
            e.Graphics.DrawString(this.txtNo.Text.Trim(), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            

            decimal sp = 0;
            decimal price1 = 0;
            decimal price2 = 0;

            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = 0, j = dataGridView; j < this.dataGridView1.RowCount; i++,j++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    _CurrentPoint.X = left;
                    _CurrentPoint.Y = 106 + i * 20;
                    int btWidth = 118;      //表头长度
                    int menWidth = btWidth;
                    string menStr = dr.Cells[0].Value.ToString();
                    int menX = (int)(_CurrentPoint.X + (menWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    int menY = (int)(_CurrentPoint.Y + (btHeight - _GoodsFont.GetHeight()) / 2);
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 248;
                    menStr = dr.Cells[2].Value.ToString();
                    menX = (int)(_CurrentPoint.X + menWidth + 4);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 39;
                    menStr = dr.Cells[10].Value.ToString();
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 41;
                    menStr = dr.Cells[7].Value.ToString();
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 43;
                    menStr = dr.Cells[4].Value.ToString();
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    menStr = dr.Cells[3].Value.ToString();
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 75;
                    menStr = decimal.Parse(dr.Cells[5].Value.ToString()).ToString("f2");
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 79;
                    menStr = decimal.Parse(dr.Cells[11].Value.ToString()).ToString("f2");
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    btWidth = 75;
                    menStr = decimal.Parse(dr.Cells[6].Value.ToString()).ToString("f2");
                    menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _GoodsFont).Width) / 2);
                    menWidth += btWidth;
                    e.Graphics.DrawString(menStr, _GoodsFont, new SolidBrush(Color.Black), new Point(menX, menY));

                    sp += decimal.Parse(dr.Cells[7].Value.ToString());
                    price1 += decimal.Parse(dr.Cells[12].Value.ToString());
                    price2 += decimal.Parse(dr.Cells[6].Value.ToString());

                    if (j != 0 && j % (pageSize - 1) == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }

            _CurrentPoint.X = left + fontLeft;
            _CurrentPoint.Y = 60+204;
            e.Graphics.DrawString(string.Format("{0} {1}", this.txtShopCode.Text.Trim(), this.cBoxShop.Text.Trim()), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X= left + 336;
            e.Graphics.DrawString(price1.ToString("f2"), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X= left + 522;
            e.Graphics.DrawString(price2.ToString("f2"), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X= left + 672;
            e.Graphics.DrawString(sp.ToString("f0"), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X= left + 774;
            e.Graphics.DrawString(this.txtTax.Text.Trim(), _GoodsFont, new SolidBrush(Color.Black), _CurrentPoint);

            //_ContentFont = new Font("宋体", 8, FontStyle.Bold);
            _CurrentPoint.Y += 28;
            _CurrentPoint.X = left + fontLeft;
            e.Graphics.DrawString(string.Format("{0} {1}", accountUser.UserCode, accountUser.Oper), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);

            _ContentFont = new Font("宋体", 6, FontStyle.Bold);
            _CurrentPoint.Y += 2;
            _CurrentPoint.X = left + 490;
            e.Graphics.DrawString("本单位郑重承诺：上述配送食品，均已履行进货检查验收法定程序，索验票证齐全", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);

            //打印底图测试使用
            tablePrint(e);

            if (dataGridView < this.dataGridView1.RowCount)
                e.HasMorePages = true;
            else
            {
                e.HasMorePages = false;
                this.dataGridView = 0;
            }
        }

        private void tablePrint(System.Drawing.Printing.PrintPageEventArgs e)
        {
            int bj = 20;        //标题行距
            
            int nkWidth = 803;  //内框长度
            int nkHeight = 204;  //内框高度
            

            string _Title = "宁波新江厦股份有限公司副食批发分公司商品销售清单";
            Font _TitleFont = new Font("宋体", 16);
            Font _ContentFont = new Font("宋体", 10);
            Point _CurrentPoint = new Point(70 + left, bj);     //标题位置

            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left;
            _CurrentPoint.Y += (int)(bj+_TitleFont.GetHeight()-4); 
            e.Graphics.DrawString("客户:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 417;     //制单日期左边距
            e.Graphics.DrawString("制单日期:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left+665;     //单据号左边距
            e.Graphics.DrawString("单据号:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.Y += (int)(_ContentFont.GetHeight()+5);
            _CurrentPoint.X = left;
            e.Graphics.DrawLine(new Pen(Color.Black, 2), _CurrentPoint.X, _CurrentPoint.Y, _CurrentPoint.X + nkWidth, _CurrentPoint.Y);
            e.Graphics.DrawLine(new Pen(Color.Black, 2), _CurrentPoint.X, _CurrentPoint.Y, _CurrentPoint.X, _CurrentPoint.Y + nkHeight);
            e.Graphics.DrawLine(new Pen(Color.Black, 2), _CurrentPoint.X + nkWidth, _CurrentPoint.Y, _CurrentPoint.X + nkWidth, _CurrentPoint.Y + nkHeight);
            e.Graphics.DrawLine(new Pen(Color.Black, 2), _CurrentPoint.X, _CurrentPoint.Y + nkHeight, _CurrentPoint.X + nkWidth, _CurrentPoint.Y + nkHeight);

            int btWidth = 118;      //表头长度
            int menWidth = btWidth;
            string menStr = "商　品　编　码";
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            int menX=(int)(_CurrentPoint.X+(menWidth-e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            int menY = (int)(_CurrentPoint.Y + (btHeight - _ContentFont.GetHeight()) / 2);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 248;
            menStr = "品　 名　 及　 规　 格";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 39;
            menStr = "包装";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 41;
            menStr = "件数";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 43;
            menStr = "数量";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            menStr = "单位";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 75;
            menStr = "含税单价";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 79;
            menStr = "未税单价";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 75;
            menStr = "含税金额";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            menWidth += btWidth;
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X + menWidth, _CurrentPoint.Y, _CurrentPoint.X + menWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            btWidth = 43;
            menStr = "备注";
            menX = (int)(_CurrentPoint.X + menWidth + (btWidth - e.Graphics.MeasureString(menStr, _ContentFont).Width) / 2);
            e.Graphics.DrawString(menStr, _ContentFont, new SolidBrush(Color.Black), new Point(menX, menY));

            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X, _CurrentPoint.Y + btHeight, _CurrentPoint.X + nkWidth, _CurrentPoint.Y + btHeight);
            e.Graphics.DrawLine(new Pen(Color.Black, 1), _CurrentPoint.X, _CurrentPoint.Y + nkHeight - btHeight, _CurrentPoint.X + nkWidth, _CurrentPoint.Y + nkHeight - btHeight);

            int endHeight = _CurrentPoint.Y + nkHeight;
            _CurrentPoint.Y += nkHeight - btHeight;
            _CurrentPoint.Y += (int)(btHeight - _ContentFont.GetHeight()) / 2;
            _CurrentPoint.X = left;
            e.Graphics.DrawString("仓库:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 240;     //合计未税金额左边距
            e.Graphics.DrawString("合计未税金额:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 453;     //含税金额左边距
            e.Graphics.DrawString("含税金额:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 630;     //件数左边距
            e.Graphics.DrawString("件数:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 736;     //税率左边距
            e.Graphics.DrawString("税率:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);


            _CurrentPoint.Y = endHeight + 8;
            _CurrentPoint.X = left;
            e.Graphics.DrawString("制单:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 138;   //发货左边距
            e.Graphics.DrawString("发货:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            _CurrentPoint.X = left + 307;   //收货单位签名盖章左边距
            e.Graphics.DrawString("收货单位签名盖章:", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);

            _CurrentPoint.Y += (int)(_ContentFont.GetHeight() + 4);
            _CurrentPoint.X = left;
            e.Graphics.DrawString("备注:限十日内提货有效", _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPack.Text, out n))
                    {
                        this.txtSL_P.Text = String.Format("{0:N0}", decimal.Parse(this.txtSL.Text.Trim()) / decimal.Parse(this.txtPack.Text.Trim()));
                    }
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                    }
                }

                if (this.txtJe.Enabled)
                {
                    this.txtJe.Focus();
                }
                else
                {
                    this.btnAddRecord.Focus();
                }
            }
        }

        private void txtJe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAddRecord.Focus();
            }
        }

        private void txtUnit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtPrice.Enabled)
                {
                    this.txtPrice.Focus();
                }
                else
                {
                    this.btnAddRecord.Focus();
                }
            }
        }
    }
}
